package anc.tankshooter.ui;

import java.util.ArrayList;

import org.andengine.entity.sprite.Sprite;

import anc.tankshooter.models.Bullet;
import anc.tankshooter.models.Enermy;
import anc.tankshooter.models.Enermy.EnermyType;
import anc.tankshooter.models.Explode;
import anc.tankshooter.models.IDymanicObject.MovingDirection;
import anc.tankshooter.ui.SceneManager.SceneType;


public class SplashScreen extends IScreen{

	private Sprite mBackgroundSprite;
	private Sprite mLogoSprite;
	private Enermy mEnermyFirst;
	private Enermy mEnermySecond;
	
	private ArrayList<Bullet> mBullets;
	private Explode mExplode;
	private boolean mIsShoot;
	private float mCoundown;
	
	public SplashScreen(){
		mBullets=new ArrayList<Bullet>();
		mIsShoot=false;
	}
	
	/**
	 * Gọi tới màn hình tiếp theo
	 */
	private void nextScreen(){
		ResourcesManager.getInstance().loadMenuResources();
		MainMenuScreen menuScreen = new MainMenuScreen();
		SceneManager.getInstance().setScene(menuScreen);
	}
	@Override
	public void createScene() {
		
		this.mBackgroundSprite = new Sprite(0, 0, mResourcesManager.mBackgroundTexture, mVertextManager);
		this.attachChild(mBackgroundSprite);
		
		mLogoSprite= new Sprite(205,100,mResourcesManager.mLogoColorTexture, mVertextManager);
		this.attachChild(mLogoSprite);
		
		mEnermyFirst=new Enermy(EnermyType.STATIC, 200,70);
		mEnermyFirst.setSpeed(1.2f);
		mEnermyFirst.setDir(MovingDirection.RIGHT);
		mEnermyFirst.setAnimation(new long[] { 100, 100, 100, 100, 100, 100, 100, 100 },
				new int[] { 0, 1, 2, 3, 4, 5, 6, 7 },true);
		attachChild(mEnermyFirst);
		
		mEnermySecond=new Enermy(EnermyType.STATIC, 570,70);
		mEnermySecond.setSpeed(1.2f);
		mEnermySecond.setDir(MovingDirection.LEFT);
		attachChild(mEnermySecond);
		
		mEnermyFirst.startBlind(true, 300f);
		mEnermySecond.startBlind(true, 300f);
		
	}

	@Override
	public boolean onBackKeyPressed() {
		
		return false;
	}

	@Override
	public SceneType getSceneType() {
		return SceneType.SCENE_SPLASH;
	}

	@Override
	public void disposeScene() {
		mBackgroundSprite.detachSelf();mBackgroundSprite.dispose();
		mEnermyFirst.detachSelf(); mEnermyFirst.dispose();
		mEnermySecond.detachSelf();mEnermySecond.dispose();
	    this.detachSelf();
	}

	@Override
	public void onUpdateScreen(float second) {
		mCoundown+=second;
		mEnermyFirst.setDir(MovingDirection.DOWN);
		mEnermySecond.setDir(MovingDirection.DOWN);
		if(mCoundown>=1){
			
			if(mEnermyFirst.getY()<220)
				mEnermyFirst.onUpdate(null,second);
			if(mEnermySecond.getY()<220)
				mEnermySecond.onUpdate(null,second);
			
			if(mEnermySecond.getY()>=220) 
				mEnermySecond.setDir(MovingDirection.LEFT);
			if(mEnermyFirst.getY()>=220)
				mEnermyFirst.setDir(MovingDirection.RIGHT);
			
			for(int i=0;i<mBullets.size();i++) {
				
				Bullet b = mBullets.get(i);
				b.onUpdate(null,second);
				if(!b.isDead()) {
					for(int j=0;j<mBullets.size();j++) {
						Bullet c = mBullets.get(j);
						
						if(i!=j && b.collidesWith(c) && !b.isDead() && !c.isDead()){
							b.setDead(true);
							c.setDead(true);
							mExplode = Explode.createExplode(0, b.getCenterX() + 16, b.getCenterY());
							attachChild(mExplode);
							mExplode.start();
						}
					}
				}
				
				if(b.isDead()) {
					b.detachSelf(); b.dispose();
					mBullets.remove(i);
					i--;
				}
			}
			
			if(mExplode!=null) {
				mExplode.onUpdate(null,second);
				if(mExplode.isDead()){
					mExplode.detachSelf(); mExplode.dispose();
					unregisterUpdateHandler(this);
					nextScreen();
				}
			}
			
			if(mEnermyFirst.getY()>=220&&mEnermySecond.getY()>=220 && !mIsShoot) {
				mBullets.add(mEnermyFirst.shoot());
				mBullets.add(mEnermySecond.shoot());
				for(Bullet b: mBullets) {
					attachChild(b);
				}
				mIsShoot=true;
			}
		}
	}
}
